<template>
    <Form ref="formValidate" class="exform" :model="formValidate" :label-width="80" :rules="ruleValidate">
		<FormItem label="项目名称" prop="name.zh"  style="width:50%">
			<Input v-model="formValidate.name.zh" placeholder="项目名称" style="width:80%">
			</Input>
		</FormItem>
		<FormItem label="立项编号" prop="num"  style="width:50%">
			<Input v-model="formValidate.num" placeholder="立项编号" style="width:80%">
			</Input>
		</FormItem>
		<FormItem label="项目课题主管单位" prop="unit.zh"  style="width:50%">
			<Input v-model="formValidate.unit.zh" placeholder="项目课题主管单位" style="width:80%">
			</Input>
		</FormItem>
		<FormItem label="项目性质及来源" prop="source.zh"  style="width:50%">
			<Input v-model="formValidate.source.zh" placeholder="项目性质及来源" style="width:80%">
			</Input>
		</FormItem>
		<FormItem label="等级" prop="inputstyle.zh"  style="width:50%">
			<Input v-model="formValidate.inputstyle.zh" placeholder="等级" style="width:80%">
			</Input>
		</FormItem>
		<FormItem label="经费" prop="fund"  style="width:50%">
			<Input v-model="formValidate.fund" placeholder="经费" style="width:60%">
			</Input>（万元）
		</FormItem>

		<FormItem label="承担时间" prop="time"  style="width:50%">
			<Col span="12">
			<DatePicker placement="right" v-model="formValidate.time" type="date"  placeholder="请选择时间" style="width: 200px" transfer></DatePicker>
			</Col>
		</FormItem>
		<FormItem label="担任角色" prop="roal.zh"  style="width:50%">
			<Input v-model="formValidate.roal.zh" placeholder="担任角色" style="width:80%">
			</Input>
		</FormItem>
		<FormItem label="完成时间" prop="entime"  style="width:50%">
			<Col span="12">
			<DatePicker placement="top" v-model="formValidate.entime" type="date"  placeholder="请选择时间" style="width: 200px" transfer></DatePicker>
			</Col>
		</FormItem>


		<div style="width:90%;">
			<FormItem style="width:100%;">
			<Button type="primary" @click="handleSubmit('formValidate')">保存</Button>
			</FormItem>
		</div>
		<div style="clear:both"></div>
		<Spin size="large" fix v-if="spinShow"></Spin>
    </Form>
</template>

<script>

  export default {

    props: {
        teacher_id: {
            type: [Number,String],
            default: null
        },
        infoId: {
            type: [Boolean,String,Number],
            default: false
        }
    },
    data () {
		return {
			infoUrl:`api/teacher/v1/upbsqualification/info/${this.infoId}`,//请求单条数据
			saveUrl:'',//保存接口
			saveType:null,//保存方式
			spinShow:false,//加载动画

			infoOld:null,
			formItem:[],


			formValidate: {
				teacher_id:this.teacher_id,
				time:'',    //承担时间
				entime:'',     //完成时间
				num:'', //立项编号
				fund:'', //经费
				shenhe:1,       //审核
				roal:{
					zh:'',
					mn:'',
					en:'',
				},    //担任角色
				unit:{
					zh:'',
					mn:'',
					en:'',
				},    //项目课题主管单位
				source:{
					zh:'',
					mn:'',
					en:'',
				},    //项目性质及来源
				inputstyle:{
					zh:'',
					mn:'',
					en:'',
				},    //等级

				name:{
					zh:'',
					mn:'',
					en:'',
				},    //项目名称
			},
			ruleValidate: {
				// 承担时间
				time: [
					{ required: true, type: 'date', message: '请选择时间', trigger: 'blur' }
				],
				// 完成时间
				entime: [
					{ required: true, type: 'date', message: '请选择时间', trigger: 'blur' }
				],
				//经费
				fund: [
					{ required: true, message: '请填写经费', trigger: 'blur' }
				],
				//立项编号
				num: [
					{ required: true, message: '请填写立项编号', trigger: 'blur' }
				],
				//项目性质及来源
				'source.zh': [
					{ required: true, message: '请填写项目性质及来源', trigger: 'blur' }
				],
				//项目课题主管单位
				'unit.zh': [
					{ required: true, message: '请填写项目课题主管单位', trigger: 'blur' }
				],
				//担任角色
				'roal.zh': [
					{ required: true, message: '请填写担任角色', trigger: 'blur' }
				],
				//等级
				'inputstyle.zh': [
					{ required: true, message: '请填写等级', trigger: 'blur' }
				],
				//项目名称
				'name.zh': [
					{ required: true, message: '请填写项目名称', trigger: 'blur' }
				],

			},  
		}
    },
    methods: {
     
        
        /************************************************************
         * 
         * 单条数据请求
         * 
         ************************************************************/
        infoData(){
            this.spinShow=true//输入框禁用  
            this.$http.get(this.infoUrl).then(val=>{
                this.infoOld = JSON.parse(JSON.stringify(val.data))
                this.formValidate.shenhe = parseInt(this.infoOld.shenhe)//用于默认选的 审核
                
                this.formValidate.time = this.infoOld.time// 承担时间
                this.formValidate.entime = this.infoOld.entime// 完成时间
                this.formValidate.num = this.infoOld.num// 立项编号
                this.formValidate.fund = this.infoOld.fund// 经费
                this.formValidate.awarddept = this.infoOld.awarddept// 授予部门

                this.formValidate.roal = this.infoOld.roal// 担任角色
                this.formValidate.unit = this.infoOld.unit// 项目课题主管单位
                this.formValidate.source = this.infoOld.source// 项目性质及来源
                this.formValidate.inputstyle = this.infoOld.inputstyle// 等级
                this.formValidate.name = this.infoOld.name// 项目名称

                this.formValidate.school_id=this.infoOld.school_id;//学校
                this.formValidate.teacher_id=this.infoOld.teacher_id;//教师  
                this.spinShow=false//输入框禁用 
                
            }).catch(err => {
				if(err.code && err.code==422) {
					this.$Message.error(err.message);
				}
                this.spinShow=false//输入框禁用 
            });
        },
		
        /************************************************************
         * 
         * 提交时
         * 
         ************************************************************/
		handleSubmit (name) {
			this.$refs[name].validate((valid) => {
				if (valid) {       
					this.spinShow=true//输入框禁用   
					this.saveType(this.saveUrl,this.formValidate).then(val=>{
						this.$emit('showParent',false)
						this.$emit('backList',true)
						this.$Message.success(val.message);
						this.spinShow=false
					}).catch(err => {
						if (err.code && err.code == 422) {
							this.$Message.error(err.message);
						}
						this.spinShow=false
					})  
				} else {
					this.$Message.error('表单验证失败!');
				}
			})
		},
    },
    created() {
        if(this.infoId==false){
            // add
            this.saveUrl='api/teacher/v1/upbsqualification/add'
            this.saveType=this.$http.post

        }else{
            // edit
            this.infoData();
            this.saveUrl=`api/teacher/v1/upbsqualification/edit/${this.infoId}`
            this.saveType=this.$http.post
        }
    },
  }
</script>
<style lang="less">
@import'./../public.less';
</style>
